﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FineSoft.Models
{
	public class File : FileItem
	{
		public override string Name
		{
			get
			{
				return System.IO.Path.GetFileName(this.Path);
			}
		}

		public override string Path
		{
			get
			{
				return base.Path;
			}
			set
			{
				base.Path = value;
				this._DesignerFilePath = null;
				this._EditorDesignerFilePath = null;
				this._UserFilePath = null;
				this._DataFilePath = null;
				this._EditorFilePath = null;
				this._AssociatedFiles = null;
			}
		}

		private File[] _AssociatedFiles;
		public File[] AssociatedFiles
		{
			get
			{
				if (this._AssociatedFiles == null)
					if (System.IO.File.Exists(this.DataFilePath))
						this._AssociatedFiles = new File[] { new File(this.BaseDirectory, this.DesignerFilePath), new File(this.BaseDirectory, this.UserFilePath) };
					else this._AssociatedFiles = new File[0];
				return this._AssociatedFiles;
			}
		}

		private string _DesignerFilePath;
		public string DesignerFilePath
		{
			get
			{
				if (this._DesignerFilePath == null)
					this._DesignerFilePath = this.Path.Substring(0, this.Path.LastIndexOf('.')) + ".designer.js";
				return this._DesignerFilePath;
			}
		}

		private string _EditorDesignerFilePath;
		public string EditorDesignerFilePath
		{
			get
			{
				if (this._EditorDesignerFilePath == null)
					this._EditorDesignerFilePath = this.Path.Substring(0, this.Path.LastIndexOf('.')) + ".editor.designer.js";
				return this._EditorDesignerFilePath;
			}
		}

		private string _UserFilePath;
		private string UserFilePath
		{
			get
			{
				if (this._UserFilePath == null)
					this._UserFilePath = this.Path.Substring(0, this.Path.LastIndexOf('.')) + ".js";
				return this._UserFilePath;
			}
		}

		private string _DataFilePath;
		private string DataFilePath
		{
			get
			{
				if (this._DataFilePath == null)
					this._DataFilePath = System.IO.Path.Combine(System.IO.Path.Combine(this.BaseDirectory.Path, "Data"), this.Path.Substring(this.BaseDirectory.Path.Length));
				return this._DataFilePath;
			}
		}

		private string _EditorFilePath;
		public string EditorFilePath
		{
			get
			{
				if (this._EditorFilePath == null)
					this._EditorFilePath = this.Path.Substring(0, this.Path.LastIndexOf('.')) + ".editor.htm";
				return this._EditorFilePath;
			}
		}

		public File(Directory baseDirectory, string path)
		{
			this.BaseDirectory = BaseDirectory;
			this.Path = path;
		}
	}
}